

package basic;

import static java.lang.Math.round;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class DateClass {
    
    private final static String dateFormat = "dd/MM/yyy";
    private final static SimpleDateFormat format = new SimpleDateFormat(dateFormat);
    private final static Scanner in = new Scanner(System.in);
    
    
    private Date date1; // means hire date
    private Date date2;  // means return day

    public Date getDate1() {
        return date1;
    }

    public void setDate1(Date date1) {
        this.date1 = date1;
    }

    public Date getDate2() {
        return date2;
    }

    public void setDate2(Date date2) {
        this.date2 = date2;
    }
    
    public DateClass(Date date1, Date date2){
        this.date1 = date1;
        this.date2 = date2;
    }
    
        
    /**
     * Function compares two dates but doesnt include hours/minutes/seconds
     * @param a Date one
     * @param b Date two
     * @return standart output for compare functions
     */
    public static int compare(Date a, Date b){
        if ( ( a.getDate()==b.getDate() ) && ( a.getMonth()==b.getMonth() ) && 
                ( a.getYear()==b.getYear() ) ) return 0;
        else return a.compareTo(b);
    }
    
    /**
     * Function returns difference between two dates in days. Works properly in situation
     * when d2>d1 or d1>d2
     * @param d1 Date one
     * @param d2 Date two
     * @return difference in days as integer value
     */
    public static int diffInDays(Date d1, Date d2){
        // need to create h1, h2 dates that doesnt contain the time elements
        Date h1 = new Date(d1.getYear(),d1.getMonth(),d1.getDate());
        Date h2 = new Date(d2.getYear(),d2.getMonth(),d2.getDate());
        return (int)( Math.abs( round( (h1.getTime()-h2.getTime())/(1000 * 60 * 60 * 24)) ) +1 );
    }
    
    
    /**
     * Function chceks if the new reservation interval doesnt overlap with 
     * intervals of previous reservations that are stored a Map included in each bike class
     * by deafult d2 MUST be larger than d1
     * @param d1 checked hire date
     * @param d2 checked return date
     * @return true - if intervals date1-date2 and d1-d2 doesnt overlap
     *              false - otherwise
     */
    public boolean check(Date d1, Date d2){
        
        return ( (compare(this.date2, d1)<0) || (compare(this.date1, d2)>0) );
            
    }
    /**
     * Function that scans input and stores entered string as a Date object
     * @return Date object with entered string
     * @throws Exception if there is no match to entered string
     */
    public static Date readDate() throws Exception {
        System.out.println("format is: "+dateFormat.toLowerCase());
        String entered = in.next();
        try {
            Date output = format.parse(entered);
            return output;
        } catch (ParseException e){
            throw new Exception(e);
        }
              
    }
    
    
}
